package com.bank.service;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bank.code.RespCode;
import com.bank.dao.AccountDao;
import com.bank.dao.BookkeepingDao;
import com.bank.dao.TransAccountListDao;
import com.bank.entity.Account;
import com.bank.entity.Bookkeeping;
import com.bank.entity.Subject;
import com.bank.entity.TransAccountList;
import com.bank.excption.CommonException;
import com.bank.util.AccountingParam;
import com.bank.util.CommonUtil;

@Service(value="bookkeepingCommon")
public class BookkeepingCommonImpl implements BookkeepingCommon{
	
	private Logger logger = Logger.getLogger(BookkeepingCommonImpl.class);
	
	@Autowired
	AccountDao accountDao;
	
	@Autowired
	BookkeepingDao  bookkeepingDao;
	
	@Autowired
	TransAccountListDao transAccountListDao;
	/**
	 * 通用记账
	 * @param bookkeepList  记账配置类
	 * 		  accountingParamList  账号信息参数
	 * 	      prams  请求参数
	 * @return Map<String, Object>
	 * @throws CommonException 
	 * 
	 * */
	@Transactional
	public Map<String, Object> common(List<AccountingParam> accountingParamList, Map<String, Object> params) throws CommonException {
		// TODO Auto-generated method stub
		String trans_code = (String)params.get("trans_code");
		List<Bookkeeping>  bookkeepList = bookkeepingDao.getBookkeepingInfo(trans_code);
		if(bookkeepList.size()==0 ){
			 throw new CommonException(RespCode.LESS_CONFIGURATION, "会计分录未配置");
		}

		//通用记账
		int i = 0;
		Bookkeeping bookkeeping = null;
		AccountingParam accountingParam; 
		logger.debug(bookkeepList.size());
		for(;i<bookkeepList.size();i++){
			String accountNo = null;
			TransAccountList  transAccountList = new TransAccountList();
			bookkeeping = bookkeepList.get(i);
			accountingParam = accountingParamList.get(i);
			transAccountList.setTrans_teller((String)params.get("teller"));   //柜员
			transAccountList.setTrans_workdate((String)params.get("work_date"));     //交易日期
			transAccountList.setTeller_serno((String)params.get("teller_serno"));
			transAccountList.setMainframe_workdate((String)params.get("work_date"));
			transAccountList.setTrans_time((String)params.get("work_date"));
			transAccountList.setTrans_code((String)params.get("trans_code"));
			transAccountList.setDcflag(bookkeeping.getCd_direct());
			transAccountList.setTrans_amount(accountingParam.getTranAmouny());
			transAccountList.setAuth_teller((String)params.get("auth_teller"));   //授权
			transAccountList.setCustomer_no((String)params.get("trans_ori"));	//交易机构
		
			if(bookkeeping.getConfig_type().equals("0")){
				Account  account = accountingParam.getAccount();
				logger.debug("account_no"+account.getNumber());
				accountNo = account.getNumber();
				transAccountList.setAccount_no(account.getNumber());//账号
				transAccountList.setSequence(bookkeeping.getSequence());
				Double balance = account.getBalance(); //获取账户余额
				if(bookkeeping.getCd_direct().equals("C"))//贷方
				{
					if(account.getBalance_direct().equals("C"))
					{
						balance = balance + accountingParam.getTranAmouny();
						account.setBalance(balance);
					}
					if(account.getBalance_direct().equals("D"))
					{							
						if(balance<accountingParam.getTranAmouny()){
							throw new  CommonException(RespCode.LACK_OF_BALANCE, "余额不足");
						}
						balance =balance - accountingParam.getTranAmouny();
					}
					
				}
				 //借方
				if(bookkeeping.getCd_direct().equals("D")){
					if(account.getBalance_direct().equals("C")){//账户余额在贷方
						logger.debug("3333");
						if(balance<accountingParam.getTranAmouny()){
							throw new  CommonException(RespCode.LACK_OF_BALANCE, "余额不足");
						}
						balance =balance - accountingParam.getTranAmouny();
						account.setBalance(balance);
					}
					
					if (account.getBalance_direct().equals("D")){//账户余额在借方
						balance = balance + accountingParam.getTranAmouny();
						account.setBalance(balance);
					}
				}
				accountDao.updateAccountBalance(account);   //更新账户余额
				transAccountList.setBalance(balance);
				transAccountList.setStatus("1");
				transAccountList.setAccount_no(accountNo);
				transAccountList.setBalance_direct(account.getBalance_direct());
				transAccountList.setSubject_number(account.getAccount_subject());
				transAccountList.setCurrenry(account.getCurrency());
			}else if(bookkeeping.getConfig_type().equals("1")){//使用配置数据
				String subject_number = null;     /*客户号*/
				String subject_name = null;		  /*客户名称*/
				String cd_direct  = bookkeeping.getCd_direct();         /*借贷方向*/
				String balance_direct = null;     /*余额方向*/
				Double balance = 0.00; //获取账户余额
				transAccountList.setSequence(bookkeeping.getSequence());
				if(bookkeeping.getSubject_type().equals("1"))//配置科目
				{	
					subject_number = bookkeeping.getSubject_number();
					subject_name = bookkeeping.getSubject_name();
					logger.debug(subject_number);
					logger.debug(subject_name);
				}else{
					
					subject_number = accountingParam.getAccount_subject();
					Subject subject =  CommonUtil.getSubjectInfo(subject_number);
					subject_name  = subject.getSubject_name();			 //科目名称
				}
				Account account = accountDao.getAccountByParams(accountingParam.getCurrency(), accountingParam.getAccounting_ori(), subject_number);
				if (account== null){
					throw new  CommonException(RespCode.LESS_CONFIGURATION, "账号不存在");
				}
				accountNo= account.getNumber();
				balance_direct  = account.getBalance_direct();
				balance = account.getBalance();
				if(cd_direct.equals("C")){//贷方
				
					if(balance_direct.equals("C")){//余额方向为贷方
						balance = balance + accountingParam.getTranAmouny();
					}
					if(balance_direct.equals("D")){//余额方向为贷方
						if(balance<accountingParam.getTranAmouny()){
							throw new  CommonException(RespCode.LACK_OF_BALANCE, "余额不足");
						}
						balance = balance - accountingParam.getTranAmouny();
					}
					
				}
				if(cd_direct.endsWith("D")){//借方
					if(balance_direct.equals("C")){//贷方
						if(balance<accountingParam.getTranAmouny()){
							throw new  CommonException(RespCode.LACK_OF_BALANCE, "余额不足");
						}
						balance = balance - accountingParam.getTranAmouny();
					}
					if(balance_direct.equals("D")){//余额方向为贷方
						balance = balance + accountingParam.getTranAmouny();
					}
				}
				account.setBalance(balance);
				accountDao.updateAccountBalance(account);   //更新账户余额
				transAccountList.setBalance(balance);
				transAccountList.setStatus("1");
				transAccountList.setAccount_no(accountNo);
				transAccountList.setBalance_direct(account.getBalance_direct());
				transAccountList.setSubject_number(account.getAccount_subject());
				transAccountList.setCurrenry(account.getCurrency());
			}
			logger.debug("开始保存会计分录");
			transAccountListDao.save(transAccountList);
		}
		return null;
	}
	
	
}
